﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data.Common;
using System.Data;
using System.Data.OleDb;

/// <summary>
///DBDelaySendWeibo 的摘要说明
/// </summary>
public class DBDelaySendWeibo
{
	public DBDelaySendWeibo()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}

    /// <summary>
    /// 添加定时发送的微博
    /// </summary>
    /// <param name="weiboId"></param>
    /// <param name="replay"></param>
    /// <param name="replayAccount"></param>
    /// <param name="sendTime"></param>
    /// <param name="addTime"></param>
    /// <param name="commentId"></param>
    /// <returns>记录的标识</returns>
    public int Add(string weiboId, string replay, string replayAccount, DateTime sendTime, DateTime addTime, string commentId)
    {
        DbCommand command = DBConfig.CreateCommand("INSERT INTO [delaySendWeibo](weiboId, reply, replyAccount, replyTime, addTime, commentId) VALUES(@weiboId, @reply, @replyAccount, @sendTime, @addTime, @commentId)");
        command.Parameters.AddRange(new DbParameter[] { 
        DBConfig.CreateParameter("@weiboId", weiboId),
        DBConfig.CreateParameter("@reply", replay),
        DBConfig.CreateParameter("@replyAccount", replayAccount),
        DBConfig.CreateParameter("@sendTime", "#" + sendTime + "#", OleDbType.DBTimeStamp),
        DBConfig.CreateParameter("@addTime", "#" + addTime + "#", OleDbType.DBTimeStamp),
        DBConfig.CreateParameter("@commentId", commentId)
        });

        DbCommand selectCommand = DBConfig.CreateCommand("Select max(id) AS newid From [delaySendWeibo]");
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            try
            {
                command.ExecuteNonQuery();
                selectCommand.Connection = conn;
                return Convert.ToInt32(selectCommand.ExecuteScalar());
            }
            catch (Exception error)
            {
                return 0;
            }
        }
    }

    /// <summary>
    /// 删除记录
    /// </summary>
    /// <param name="id"></param>
    public void Delete(int id)
    {
        DbCommand command = DBConfig.CreateCommand("Delete From [delaySendWeibo] Where id=@id");
        command.Parameters.Add(DBConfig.CreateParameter("@id", id));
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            command.ExecuteNonQuery();
        }
    }

    public void Update(int id, string reply)
    {
        DbCommand command = DBConfig.CreateCommand("Update [delaySendWeibo] Set reply=@reply Where id=@id");
        command.Parameters.AddRange(
            new DbParameter[]
            {
                DBConfig.CreateParameter("@reply", reply),
                DBConfig.CreateParameter("@id", id)
            }
            );
            using (DbConnection conn = DBConfig.CreateConnect())
            {
                command.Connection = conn;
                conn.Open();
                command.ExecuteNonQuery();
            }
    }

    /// <summary>
    /// 批量删除要发送的微博
    /// </summary>
    /// <param name="blogAccount">发送微博使用的帐号</param>
    /// <returns></returns>
    public bool DeleteList(string replyAccount)
    {
        DbCommand command = DBConfig.CreateCommand("Delete FROM [delaySendWeibo] WHERE replyAccount= @replyAccount");
        command.Parameters.Add(DBConfig.CreateParameter("@replyAccount", replyAccount));
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            return command.ExecuteNonQuery() > 0;
        }
    }

    /// <summary>
    /// 查找
    /// </summary>
    /// <param name="weiboId">原始微博ID</param>
    /// <returns></returns>
    public DelayWeiboData Select(string weiboId)
    {
        DbCommand command = DBConfig.CreateCommand("SELECT * FROM [delaySendWeibo] WHERE weiboId = @weiboId");
        command.Parameters.Add(DBConfig.CreateParameter("@weiboId", weiboId));
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            DbDataReader reader = command.ExecuteReader();
            if (reader.Read())
            {
                return ReadData(reader);
            }
            else
            {
                return null;
            }
        }
    }

    /// <summary>
    /// 查找
    /// </summary>
    /// <param name="weiboId">记录ID</param>
    /// <returns></returns>
    public DelayWeiboData Select(int id)
    {
        DbCommand command = DBConfig.CreateCommand("SELECT * FROM [delaySendWeibo] WHERE id = @id");
        command.Parameters.Add(DBConfig.CreateParameter("@id", id));
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            DbDataReader reader = command.ExecuteReader();
            if (reader.Read())
            {
                return ReadData(reader);
            }
            else
            {
                return null;
            }
        }
    }

    public List<DelayWeiboData> SelectList(string weiboId)
    {
        DbCommand command = DBConfig.CreateCommand("SELECT * FROM [delaySendWeibo] WHERE weiboId = @weiboId");
        command.Parameters.Add(DBConfig.CreateParameter("@weiboId", weiboId));
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            DbDataReader reader = command.ExecuteReader();
            List<DelayWeiboData> result = null;
            while (reader.Read())
            {
                if (result == null)
                    result = new List<DelayWeiboData>();
                result.Add(ReadData(reader));
            }
            return result;
        }
    }

    /// <summary>
    /// 选出设置的回复时间早于指定时定的微博
    /// </summary>
    /// <param name="lastReplyTime"></param>
    /// <returns></returns>
    public List<DelayWeiboData> SelectList(DateTime lastReplyTime)
    {
        DbCommand command = DBConfig.CreateCommand("Select * From [delaySendWeibo] Where replyTime < @replyTime");
        command.Parameters.Add(DBConfig.CreateParameter("@replyTime", "#" + lastReplyTime + "#", OleDbType.DBTimeStamp));
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            DbDataReader reader = command.ExecuteReader();
            List<DelayWeiboData> result = null;
            while (reader.Read())
            {
                if (result == null)
                    result = new List<DelayWeiboData>();
                result.Add(ReadData(reader));
            }
            return result;
        }
    }

    public List<DelayWeiboData> SelectList()
    {
        DbCommand command = DBConfig.CreateCommand("Select * From [delaySendWeibo]");
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            DbDataReader reader = command.ExecuteReader();
            List<DelayWeiboData> result = null;
            while (reader.Read())
            {
                if (result == null)
                    result = new List<DelayWeiboData>();
                result.Add(ReadData(reader));
            }
            return result;
        }
    }

    public bool DeleteBefore(DateTime lastTime)
    {
        DbCommand command = DBConfig.CreateCommand("Delete FROM [delaySendWeibo] WHERE addTime >= @lastTime");
        command.Parameters.Add(DBConfig.CreateParameter("@lastTime", lastTime));
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            return command.ExecuteNonQuery() > 0;
        }
    }

    private DelayWeiboData ReadData(DbDataReader reader)
    {
        DelayWeiboData result = new DelayWeiboData();
        result.id = reader.GetInt32(0);
        result.weiboId = reader.GetString(1);
        result.reply = reader.GetString(2);
        result.replyAccount = reader.GetString(3);
        result.replyTime = reader.GetDateTime(4);
        result.addTime = reader.GetDateTime(5);
        result.commentId = reader.GetString(6);
        return result;
    }
}